package com.enthusiasm.constants;

import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.young.R;

public class BitmapManager {

	private static BitmapManager instance;
	private static Object INSTANCE_LOCK = new Object();
	private Context context;
	// Menu
	public Bitmap bmpGameTitle;
	public Bitmap bmpBackground;
	public Bitmap bmpGameStart_before;
	public Bitmap bmpGameStart_after;
	public Bitmap bmpMedia_open;
	public Bitmap bmpMeida_close;
	public Bitmap bmpSound_open;
	public Bitmap bmpSound_close;
	public Bitmap bmpTree;
	public Bitmap bmpGrass;
	public Bitmap bmpSun;
	public Bitmap bmpWolf01;
	public Bitmap bmpWolf02;
	public Bitmap bmpPuzzle;
	// ModeMenu
	public Bitmap bmpScreen;
	public Bitmap bmpBack01;
	public Bitmap bmpBack02;
	public Bitmap bmpCollect01;
	public Bitmap bmpCollect02;
	// Collect
	public ArrayList<Bitmap> bmpCollect_lock;
	public ArrayList<Bitmap> bmpCollect_unlock;
	public Bitmap bmpCollectLock;
	public Bitmap bmpScreenCollect;
	public Bitmap bmpQuestion01;
	public Bitmap bmpQuestion02;
	public Bitmap bmpCollectNotice;
	// Mode01
	public Bitmap bmpScreen01;
	public Bitmap bmpLevelBg;
	public Bitmap bmpLevelLock;
	public Bitmap bmpLevelOpen;
	public Bitmap bmpLevelNum;
	// Mode02
	public Bitmap bmpScreen02;
	// Mode03
	public Bitmap bmpScreen03;
	// Mode04
	public Bitmap bmpScreen04;
	// Mode05
	public Bitmap bmpScreen05;

	public Bitmap readBitMap(int resId) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		// 获取资源图片
		InputStream is = context.getResources().openRawResource(resId);
		return BitmapFactory.decodeStream(is, null, opt);
	}

	public BitmapManager(Context context) {
		this.context = context;
		// initMenuBitmap();
		// initModeBitmap();
		// initMode01Bitmap();
		// initMode02Bitmap();
		// initMode03Bitmap();
		// initMode04Bitmap();
		// initMode05Bitmap();
	}

	public void initMode01Bitmap() {
		if (bmpScreen01 == null)
			bmpScreen01 = readBitMap(R.drawable.gamemode_one_background);
		if (bmpBack01 == null)
			bmpBack01 = readBitMap(R.drawable.button_back_before);
		if (bmpBack02 == null)
			bmpBack02 = readBitMap(R.drawable.button_back_after);
		if (bmpLevelBg == null)
			bmpLevelBg = readBitMap(R.drawable.gamemode_level_bg);
		if (bmpLevelLock == null)
			bmpLevelLock = readBitMap(R.drawable.gamemode_level_lock);
		if (bmpLevelNum == null)
			bmpLevelNum = readBitMap(R.drawable.gamemode_level_num);
	}

	public void releaseMode01Bitmap() {
		if (bmpScreen01 != null) {
			bmpScreen01.recycle();
			bmpScreen01 = null;
		}
		if (bmpBack01 != null) {
			bmpBack01.recycle();
			bmpBack01 = null;
		}
		if (bmpBack02 != null) {
			bmpBack02.recycle();
			bmpBack02 = null;
		}
		if (bmpLevelBg != null) {
			bmpLevelBg.recycle();
			bmpLevelBg = null;
		}
		if (bmpLevelLock != null) {
			bmpLevelLock.recycle();
			bmpLevelLock = null;
		}
		if (bmpLevelNum != null) {
			bmpLevelNum.recycle();
			bmpLevelNum = null;
		}
	}

	public void initMode02Bitmap() {
		if (bmpScreen02 == null)
			bmpScreen02 = readBitMap(R.drawable.gamemode_two_background);
		if (bmpBack01 == null)
			bmpBack01 = readBitMap(R.drawable.button_back_before);
		if (bmpBack02 == null)
			bmpBack02 = readBitMap(R.drawable.button_back_after);
		if (bmpLevelBg == null)
			bmpLevelBg = readBitMap(R.drawable.gamemode_level_bg);
		if (bmpLevelLock == null)
			bmpLevelLock = readBitMap(R.drawable.gamemode_level_lock);
		if (bmpLevelNum == null)
			bmpLevelNum = readBitMap(R.drawable.gamemode_level_num);
	}

	public void releaseMode02Bitmap() {
		if (bmpScreen02 != null) {
			bmpScreen02.recycle();
			bmpScreen02 = null;
		}
		if (bmpBack01 != null) {
			bmpBack01.recycle();
			bmpBack01 = null;
		}
		if (bmpBack02 != null) {
			bmpBack02.recycle();
			bmpBack02 = null;
		}
		if (bmpLevelBg != null) {
			bmpLevelBg.recycle();
			bmpLevelBg = null;
		}
		if (bmpLevelLock != null) {
			bmpLevelLock.recycle();
			bmpLevelLock = null;
		}
		if (bmpLevelNum != null) {
			bmpLevelNum.recycle();
			bmpLevelNum = null;
		}
	}

	public void initMode03Bitmap() {
		if (bmpScreen03 == null)
			bmpScreen03 = readBitMap(R.drawable.gamemode_three_background);
		if (bmpBack01 == null)
			bmpBack01 = readBitMap(R.drawable.button_back_before);
		if (bmpBack02 == null)
			bmpBack02 = readBitMap(R.drawable.button_back_after);
		if (bmpLevelBg == null)
			bmpLevelBg = readBitMap(R.drawable.gamemode_level_bg);
		if (bmpLevelLock == null)
			bmpLevelLock = readBitMap(R.drawable.gamemode_level_lock);
		if (bmpLevelNum == null)
			bmpLevelNum = readBitMap(R.drawable.gamemode_level_num);
	}

	public void releaseMode03Bitmap() {
		bmpScreen03.recycle();
		bmpScreen03 = null;
		if (bmpBack01 != null) {
			bmpBack01.recycle();
			bmpBack01 = null;
		}
		if (bmpBack02 != null) {
			bmpBack02.recycle();
			bmpBack02 = null;
		}
		if (bmpLevelBg != null) {
			bmpLevelBg.recycle();
			bmpLevelBg = null;
		}
		if (bmpLevelLock != null) {
			bmpLevelLock.recycle();
			bmpLevelLock = null;
		}
		if (bmpLevelNum != null) {
			bmpLevelNum.recycle();
			bmpLevelNum = null;
		}
	}

	public void initMode04Bitmap() {
		if (bmpScreen04 == null)
			bmpScreen04 = readBitMap(R.drawable.gamemode_four_background);
		if (bmpBack01 == null)
			bmpBack01 = readBitMap(R.drawable.button_back_before);
		if (bmpBack02 == null)
			bmpBack02 = readBitMap(R.drawable.button_back_after);
		if (bmpLevelBg == null)
			bmpLevelBg = readBitMap(R.drawable.gamemode_level_bg);
		if (bmpLevelLock == null)
			bmpLevelLock = readBitMap(R.drawable.gamemode_level_lock);
		if (bmpLevelNum == null)
			bmpLevelNum = readBitMap(R.drawable.gamemode_level_num);
	}

	public void releaseMode04Bitmap() {
		bmpScreen04.recycle();
		bmpScreen04 = null;
		if (bmpBack01 != null) {
			bmpBack01.recycle();
			bmpBack01 = null;
		}
		if (bmpBack02 != null) {
			bmpBack02.recycle();
			bmpBack02 = null;
		}
		if (bmpLevelBg != null) {
			bmpLevelBg.recycle();
			bmpLevelBg = null;
		}
		if (bmpLevelLock != null) {
			bmpLevelLock.recycle();
			bmpLevelLock = null;
		}
		if (bmpLevelNum != null) {
			bmpLevelNum.recycle();
			bmpLevelNum = null;
		}
	}

	public void initMode05Bitmap() {
		// if(bmpScreen05 == null){
		// bmpScreen05 = readBitMap(
		// R.drawable.gamemode_five_background);
		// }
		if (bmpBack01 == null)
			bmpBack01 = readBitMap(R.drawable.button_back_before);
		if (bmpBack02 == null)
			bmpBack02 = readBitMap(R.drawable.button_back_after);
		if (bmpLevelBg == null) {
			bmpLevelBg = readBitMap(R.drawable.gamemode_level_bg);
		}
		if (bmpLevelLock == null) {
			bmpLevelLock = readBitMap(R.drawable.gamemode_level_lock);
		}
		if (bmpLevelNum == null) {
			bmpLevelNum = readBitMap(R.drawable.gamemode_level_num);
		}
	}

	public void releaseMode05Bitmap() {
		bmpScreen05.recycle();
		bmpScreen05 = null;
		if (bmpBack01 != null) {
			bmpBack01.recycle();
			bmpBack01 = null;
		}
		if (bmpBack02 != null) {
			bmpBack02.recycle();
			bmpBack02 = null;
		}
		if (bmpLevelBg != null) {
			bmpLevelBg.recycle();
			bmpLevelBg = null;
		}
		if (bmpLevelLock != null) {
			bmpLevelLock.recycle();
			bmpLevelLock = null;
		}
		if (bmpLevelNum != null) {
			bmpLevelNum.recycle();
			bmpLevelNum = null;
		}
	}

	public void initModeBitmap() {
		if (bmpScreen == null)
			bmpScreen = readBitMap(R.drawable.gamemode_background);
		if (bmpBack01 == null)
			bmpBack01 = readBitMap(R.drawable.button_back_before);
		if (bmpBack02 == null)
			bmpBack02 = readBitMap(R.drawable.button_back_after);
		if (bmpCollect01 == null)
			bmpCollect01 = readBitMap(R.drawable.button_collect_before);
		if (bmpCollect02 == null)
			bmpCollect02 = readBitMap(R.drawable.button_collect_after);
	}

	public void releaseModeBitmap() {
		if (bmpScreen != null) {
			bmpScreen.recycle();
			bmpScreen = null;
		}
		if (bmpBack01 != null) {
			bmpBack01.recycle();
			bmpBack01 = null;
		}
		if (bmpBack02 != null) {
			bmpBack02.recycle();
			bmpBack02 = null;
		}
		if (bmpCollect01 != null) {
			bmpCollect01.recycle();
			bmpCollect01 = null;
		}
		if (bmpCollect02 != null) {
			bmpCollect02.recycle();
			bmpCollect02 = null;
		}
		System.gc();
	}

	public void initMenuBitmap() {
		if (bmpGameTitle == null)
			bmpGameTitle = readBitMap(R.drawable.gamemenu_title);
		if (bmpBackground == null)
			bmpBackground = readBitMap(R.drawable.gamemenu_background);
		if (bmpGameStart_before == null)
			bmpGameStart_before = readBitMap(R.drawable.gamemenu_startgame01);
		if (bmpGameStart_after == null)
			bmpGameStart_after = readBitMap(R.drawable.gamemenu_startgame02);
		if (bmpMedia_open == null)
			bmpMedia_open = readBitMap(R.drawable.music_open);
		if (bmpMeida_close == null)
			bmpMeida_close = readBitMap(R.drawable.music_close);
		if (bmpSound_open == null)
			bmpSound_open = readBitMap(R.drawable.sound_open);
		if (bmpSound_close == null)
			bmpSound_close = readBitMap(R.drawable.sound_close);
		if (bmpTree == null)
			bmpTree = readBitMap(R.drawable.tree);
		if (bmpGrass == null)
			bmpGrass = readBitMap(R.drawable.gamemenu_grass);
		if (bmpSun == null)
			bmpSun = readBitMap(R.drawable.sun);
		if (bmpWolf01 == null)
			bmpWolf01 = readBitMap(R.drawable.wolf_female01);
		if (bmpWolf02 == null)
			bmpWolf02 = readBitMap(R.drawable.wolf_female02);
		if (bmpPuzzle == null)
			bmpPuzzle = readBitMap(R.drawable.font_puzzle);
	}

	public void releaseMenuBitmap() {
		if (bmpGameTitle != null) {
			bmpGameTitle.recycle();
			bmpGameTitle = null;
		}
		if (bmpBackground != null) {
			bmpBackground.recycle();
			bmpBackground = null;
		}
		if (bmpGameStart_before != null) {
			bmpGameStart_before.recycle();
			bmpGameStart_before = null;
		}
		if (bmpGameStart_after != null) {
			bmpGameStart_after.recycle();
			bmpGameStart_after = null;
		}
		if (bmpMedia_open != null) {
			bmpMedia_open.recycle();
			bmpMedia_open = null;
		}
		if (bmpMeida_close != null) {
			bmpMeida_close.recycle();
			bmpMeida_close = null;
		}
		if (bmpSound_open != null) {
			bmpSound_open.recycle();
			bmpSound_open = null;
		}
		if (bmpSound_close != null) {
			bmpSound_close.recycle();
			bmpSound_close = null;
		}
		if (bmpTree != null) {
			bmpTree.recycle();
			bmpTree = null;
		}
		if (bmpGrass != null) {
			bmpGrass.recycle();
			bmpGrass = null;
		}
		if (bmpSun != null) {
			bmpSun.recycle();
			bmpSun = null;
		}
		if (bmpWolf01 != null) {
			bmpWolf01.recycle();
			bmpWolf01 = null;
		}
		if (bmpWolf02 != null) {
			bmpWolf02.recycle();
			bmpWolf02 = null;
		}
		System.gc();
	}

	int lock[] = { R.drawable.collect01, R.drawable.collect02,
			R.drawable.collect03, R.drawable.collect04, R.drawable.collect };
	int unlock[] = { R.drawable.collect05, R.drawable.collect06,
			R.drawable.collect07, R.drawable.collect08,
			R.drawable.collect_final };

	public void initCollectBitmap() {
		if (bmpBack01 == null)
			bmpBack01 = readBitMap(R.drawable.button_back_before);
		if (bmpBack02 == null)
			bmpBack02 = readBitMap(R.drawable.button_back_after);
		bmpCollect_unlock = new ArrayList<Bitmap>();
		for (int i = 0; i < unlock.length; i++) {
			Bitmap bmp = readBitMap(unlock[i]);
			bmpCollect_unlock.add(bmp);
		}

		bmpCollect_lock = new ArrayList<Bitmap>();
		for (int i = 0; i < lock.length; i++) {
			Bitmap bmp = readBitMap(lock[i]);
			bmpCollect_lock.add(bmp);
		}
		bmpScreenCollect = readBitMap(R.drawable.gamecollect_background);
		bmpQuestion01 = readBitMap(R.drawable.button_question_before);
		bmpQuestion02 = readBitMap(R.drawable.button_question_after);
		bmpCollectNotice = readBitMap(R.drawable.gamecollect_notice);
	}

	public void releaseCollectBitmap() {
		bmpCollect_lock.clear();
		bmpCollect_lock = null;
		if (bmpBack01 != null) {
			bmpBack01.recycle();
			bmpBack01 = null;
		}
		if (bmpBack02 != null) {
			bmpBack02.recycle();
			bmpBack02 = null;
		}
		if (bmpQuestion01 != null) {
			bmpQuestion01.recycle();
			bmpQuestion01 = null;
		}
		if (bmpQuestion02 != null) {
			bmpQuestion02.recycle();
			bmpQuestion02 = null;
		}
		bmpCollectNotice.recycle();
		bmpCollectNotice = null;
	}

	public static BitmapManager getInstance(Context context) {
		synchronized (INSTANCE_LOCK) {
			if (instance == null) {
				instance = new BitmapManager(context);
			}
		}
		return instance;
	}
}
